package org.rufolfheszele.andpixelbyandpixel.configuration;

import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This class is holding configuration data related to a separate level
 *
 * @author Rudolf Heszele heszele@gmail.com
 * @version 1.0
 * @since 2012-03-08
 */
public class Level
{
		/**
		 * The only constructor takes the XML node and reads every data into its data members
		 *
		 * @param node The XML node containing the network configuration
		 */
		public Level(Node node)
		{
			NodeList children = node.getChildNodes();
			NamedNodeMap attributes = node.getAttributes();

			mNode = node;
			mName = null;
			for(int i = 0; i < attributes.getLength(); ++i)
			{
				Attr attribute = (Attr)attributes.item(i);
				
				if(true == attribute.getName().equals("name"))
				{
					mName = attribute.getValue();
					break;
				}
			}
			if(null == mName)
				mName = "name";
			for(int i = 0; i < children.getLength(); ++i)
			{
				Node child = children.item(i);
				
				if(true == child.getNodeName().equals("vehicle"))
				{
					Vehicle vehicle = new Vehicle(child);
					
					if(true == vehicle.getName().equals("A"))
						mVehicleA = vehicle;
					else if(true == vehicle.getName().equals("B"))
						mVehicleB = vehicle;
				}
			}
		}
		
		public String getName()
		{
			return mName;
		}
		
		public void setName(String name)
		{
			NamedNodeMap attributes = mNode.getAttributes();

			mName = name;
			for(int i = 0; i < attributes.getLength(); ++i)
			{
				Attr attribute = (Attr)attributes.item(i);
				
				if(true == attribute.getName().equals("name"))
				{
					attribute.setValue(name);
					break;
				}
			}
			
		}
		
		public Vehicle getVehicleA()
		{
			return mVehicleA;
		}
		
		public Vehicle getVehicleB()
		{
			return mVehicleB;
		}

		private String mName		= null;
		private Vehicle mVehicleA	= null;
		private Vehicle mVehicleB	= null;
		private Node mNode			= null;
};